home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Especial Multimedia
/
Especial Multimedia.iso
/
Multimed
/
Fuentes
/
TTFPFX2.ZIP
/
TTFPFX2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-14
|
5KB
|
164 lines
/**************************************
ttfpfx2.c TTF (True Type Font) Prefix Patch
***************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <io.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <direct.h>
#include <dos.h>
typedef int BOOL ; /* logical YES or NO */
typedef unsigned char BYTE ; /* binary file I/O */
typedef char* STRN ; /* STRing Null terminated */
static FILE *inpfil ; /* input prefix =new=old=file1.TTF .. */
static FILE *msgfil ; /* diagnostic output file (inpfil).FX2 */
static char data[800] ; /* buffer for old and new datums */
static char *dap ; /* free-end ptr into data */
static char *xold[8] ; /* old ptrs into data */
static char *xnew[8] ; /* new ptrs into data */
static int xlen[8] ; /* len of datums */
static int endix ; /* number of datums */
/*************************/
static void patdoc(void)
{
fprintf(stderr, " Single arg must be ascii input file name \n") ;
fprintf(stderr, " Error... See TTFPFX2.DOC \n" ) ;
exit(1) ;
}
/****************************************/
static FILE* jopen(char *nam, char *mode)
{
FILE *hand = fopen(nam, mode) ;
if(NULL != hand) return(hand) ;
fprintf(stderr," Unable to open file %s %s \n", nam,mode) ;
patdoc() ;
}
/********************************************/
static void serfii( char *fmt, int i1, int i2)
{
fprintf(msgfil, fmt, i1,i2) ; fputc( 10, msgfil) ;
exit(2) ;
}
/*****************************************/
static void seeklog(int hand, long offlog)
{
long log = lseek(hand, offlog, SEEK_SET) ;
if(log == offlog) return ;
serfii("lseek err %d%d",0,0) ;
}
/***************************/
static void repx(STRN filna) /* file name to patch */
{
#define MUL512 30720
static char buf[MUL512+300] ;
char *bp, *end, c0 = *xold[0] ;
long offend, fillen , offlog = 0 ;
int hand, was, buflen, xx , knt = 0 ;
hand = open(filna, O_BINARY|O_RDWR) ;
if(-1 == hand)
{ fprintf(msgfil," Error opening %s \n", filna) ;
return ;
}
fillen = filelength(hand) ;
for( ; offlog<fillen ; offlog += MUL512 )
{ seeklog(hand, offlog) ;
offend = offlog + MUL512 + xlen[1] ;
if(fillen<offend) offend = fillen ;
buflen = offend - offlog ;
was = read(hand, buf, buflen) ;
if(was != buflen) serfii("%d=read(%d)", was, buflen) ;
was = knt ; bp=buf ; end = buf + buflen - xlen[0] ;
for( ; bp<=end ; ++bp )
if(c0==*bp)
for( xx=0 ; xx<endix ; ++xx)
if(!memcmp(bp,xold[xx],xlen[xx]))
{ memcpy(bp,xnew[xx],xlen[xx]) ;
++knt ; bp += (xlen[xx]-1) ; break ;
}
if(was<knt)
{ seeklog(hand, offlog ) ;
was = write(hand, buf, buflen) ;
if(was != buflen) serfii("%d=write(%d)", was, buflen) ;
}
}
close(hand) ;
fprintf(msgfil,"Did %3d %-12s <= %-12s IN %-12s \n", knt,xnew[0],xold[0], filna) ;
}
/*******************************/
STRN dacopy(STRN ins)
{
char *pold = dap ;
while( *dap++ = *ins++) ;
return(pold) ;
}
/*******************************/
char *dacopz(STRN ins)
{
char *pold = dap ;
while( *dap++ = *ins++) *dap++ = 0 ;
return(pold) ;
}
/*************************/
static void logger(STRN new, STRN old, int len)
{
xnew[endix] = dacopy(new) ;
xold[endix] = dacopy(old) ;
xlen[endix++] = len ;
xnew[endix] = dacopz(new) ;
xold[endix] = dacopz(old) ;
xlen[endix++] = (2*len)-1 ;
}
/*******************************/
STRN delspace(STRN ins)
{
char *pold = dap ; char cc ;
while( *dap++ = cc = *ins++) if(' '==cc) --dap ;
return(pold) ;
}
/*******************************/
int main(int argc, char *argv[])
{
char inp[340] ; char sep[8] ; char outna[40] ;
int len1, len2 ;
STRN inew, iold, ifil ;
if(2 != argc) patdoc() ;
inpfil = jopen(argv[1],"r") ;
strncpy(outna, argv[1], strcspn(argv[1],".")) ;
strcat(outna, ".FX2") ;
msgfil = jopen(outna, "w") ;
while(NULL != fgets(inp,330,inpfil))
{ if ( 12 > strlen(inp)) continue ;
endix = 0 ; dap = data ;
sep[0] = inp[0] ; sep[1] = '\n' ; sep[2] = 0 ;
inew = strtok(inp+1, sep) ;
iold = strtok(NULL , sep) ;
len1 = strlen(iold) ;
if(len1 != strlen(inew))
{ fprintf(msgfil," Error unequal len %s %s \n",iold,inew) ;
continue ;
}
logger(inew, iold, len1) ;
len2 = strlen(iold = delspace(iold)) ;
if(len1 != len2) logger( delspace(inew) ,iold, len2) ;
while(NULL != (ifil = strtok(NULL , sep))) repx(ifil) ;
}
fclose(inpfil) ; fclose(msgfil) ;
exit(0) ;
}